<?php
namespace app\task\controller;
use app\task\logic\WrFormDepartmentDayLogic;

/**
 * 工作记录部门报表（天）api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/20
 * Time: 14:28
 */
class WrFormDepartmentDay extends Base {

    public function __construct(){
        parent::__construct();
        $this->wfdd_logic = new WrFormDepartmentDayLogic();
    }

    /**
     * 处理报表
     * @return \think\response\Json
     */
    public function deal_form(){
        $h = new \app\common\controller\Holiday();

        $maps = [];
        $maps[] = ['department','<>',''];
        $list = model('user')->get_column($maps,'DISTINCT department','','','','','',false);
        foreach($list as $k => $v){
            //获取需要计算的时间
            $r = $this->wfdd_logic->get_count_time($v);
            if(!$r['code']){
                continue;
            }
            $start_time = $r['data']['start_time'];
            $end_time = $r['data']['end_time'];
            $timeid = $r['data']['timeid'];

            $rh = $h->get_date_holiday($timeid);

            //查找该月是否已计算数据
            $maps = [];
            $maps[] = ['department','=',$v];
            $maps[] = ['timeid','=',$timeid];
            $wrfd_detail = model('wr_form_department_day')->get_detail($maps,'id');

            //计算发布次数
            $release = $this->wfdd_logic->count_release_num($v,$start_time,$end_time);

            $wrfd_data = [];
            $wrfd_data['department'] = $v;
            $wrfd_data['extend_release_num'] = $release['extend_release_num'];
            $wrfd_data['daily_release_num'] = $release['daily_release_num'];
            $wrfd_data['active_release_num'] = $release['active_release_num'];
            $wrfd_data['notice_release_num'] = $release['notice_release_num'];
            $wrfd_data['data_release_num'] = $release['data_release_num'];
            $wrfd_data['holiday'] = $rh['holiday'];
            $wrfd_data['timeid'] = $timeid;

            //更新数据
            if(empty($cfbm_detail)){
                $r = model('wr_form_department_day')->insert($wrfd_data);
            }else{
                $maps = [];
                $maps[] = ['id','=',$wrfd_detail['id']];
                $r = model('wr_form_department_day')->where($maps)->update($wrfd_data);
            }
        }
        return json($this->wfdd_logic->get_response(1,'处理工作记录部门报表数：' . count($list)));
    }
}